package jy.service.impl;

import java.util.Date;
import java.util.List;


import jy.dao.ResourceDaoI;
import jy.dao.ResourceTypeDaoI;
import jy.dao.RoleDaoI;
import jy.dao.UserDaoI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jy.model.Tresource;
import jy.model.Tresourcetype;
import jy.model.Trole;
import jy.model.Tuser;
import jy.service.InitServiceI;
import jy.util.MD5Util;

@Service
public class InitServiceImpl implements InitServiceI {

	@Autowired
	private UserDaoI userDao;

	@Autowired
	private RoleDaoI roleDao;

	@Autowired
	private ResourceDaoI resourceDao;

	@Autowired
	private ResourceTypeDaoI resourceTypeDao;


	@Override
	synchronized public void init() {


		initResourceType();// 初始化资源类型

		initResource();// 初始化资源

		initRole();// 初始化角色

		initUser();// 初始化用户

	}



	private void initResource() {
		Tresourcetype menuType = resourceTypeDao.get(Tresourcetype.class, "0");// 菜单类型
		Tresourcetype funType = resourceTypeDao.get(Tresourcetype.class, "1");// 功能类型


		
		Tresource cxgl = new Tresource();
		cxgl.setId("cxgl");
		cxgl.setName("储蓄管理");
		cxgl.setTresourcetype(menuType);
		cxgl.setSeq(0);
		cxgl.setUrl("/userController/cxmanager");
		cxgl.setIcon("plugin");
		cxgl.setRemark("储蓄管理");
		resourceDao.saveOrUpdate(cxgl);
		
		

		
		Tresource xykgl = new Tresource();
		xykgl.setId("xykgl");
		xykgl.setName("信用卡管理");
		xykgl.setTresourcetype(menuType);
		xykgl.setSeq(1);
		xykgl.setUrl("/creditController/manager");
		xykgl.setIcon("database_gear");
		xykgl.setRemark("信用卡管理");
		resourceDao.saveOrUpdate(xykgl);
		
		Tresource dkgl = new Tresource();
		dkgl.setId("dkgl");
		dkgl.setName("贷款管理");
		dkgl.setTresourcetype(menuType);
		dkgl.setSeq(2);
		dkgl.setIcon("plugin");
		dkgl.setRemark("贷款管理");
		resourceDao.saveOrUpdate(dkgl);


		Tresource xtgl = new Tresource();
		xtgl.setId("xtgl");
		xtgl.setName("系统管理");
		xtgl.setTresourcetype(menuType);
		xtgl.setSeq(3);
		xtgl.setIcon("plugin");
		resourceDao.saveOrUpdate(xtgl);

		Tresource sjgl = new Tresource();
		sjgl.setId("sjgl");
		sjgl.setName("数据管理");
		sjgl.setTresourcetype(menuType);
		sjgl.setTresource(xtgl);
		sjgl.setSeq(1);
		sjgl.setUrl("/dataController/manager");
		sjgl.setIcon("status_online");
		resourceDao.saveOrUpdate(sjgl);
		
		Tresource yhgl = new Tresource();
		yhgl.setId("yhgl");
		yhgl.setName("用户管理");
		yhgl.setTresourcetype(menuType);
		yhgl.setTresource(xtgl);
		yhgl.setSeq(2);
		yhgl.setUrl("/userController/manager");
		yhgl.setIcon("status_online");
		resourceDao.saveOrUpdate(yhgl);


		Tresource olgl = new Tresource();
		olgl.setId("olgl");
		olgl.setName("网上一卡通管理");
		olgl.setTresourcetype(menuType);
		olgl.setSeq(4);
		olgl.setUrl("/userController/olmanager");
		olgl.setIcon("status_online");
		resourceDao.saveOrUpdate(olgl);


		Tresource olxykgl = new Tresource();
		olxykgl.setId("olxykgl");
		olxykgl.setName("网上信用卡管理");
		olxykgl.setTresourcetype(menuType);
		olxykgl.setSeq(5);
		olxykgl.setUrl("/userController/olxykmanager");
		olxykgl.setIcon("status_online");
		resourceDao.saveOrUpdate(olxykgl);

	



	}

	private void initResourceType() {
		Tresourcetype t = new Tresourcetype();
		t.setId("0");
		t.setName("菜单");
		resourceTypeDao.saveOrUpdate(t);

		Tresourcetype t2 = new Tresourcetype();
		t2.setId("1");
		t2.setName("功能");
		resourceTypeDao.saveOrUpdate(t2);
	}

	private void initRole() {
		Trole superAdmin = new Trole();
		superAdmin.setId("0");
		superAdmin.setName("超管");
		superAdmin.getTresources().addAll(resourceDao.find("from Tresource t"));// 让超管可以访问所有资源
		superAdmin.setSeq(0);
		superAdmin.setRemark("超级管理员角色，拥有系统中所有的资源访问权限");
		roleDao.saveOrUpdate(superAdmin);

		Trole cxAdmin = new Trole();
		cxAdmin.setId("cxAdmin");
		cxAdmin.setName("储蓄管理员");
		cxAdmin.setTrole(superAdmin);// 父级是超管
		cxAdmin.setSeq(1);
		cxAdmin.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('cxgl') or t.id in ('cxgl')"));
		roleDao.saveOrUpdate(cxAdmin);

		Trole xykAdmin = new Trole();
		xykAdmin.setId("xykAdmin");
		xykAdmin.setName("信用卡管理员");
		xykAdmin.setTrole(superAdmin);// 父级是超管
		xykAdmin.setSeq(2);
		xykAdmin.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('xykgl') or t.id in ('xykgl')"));
		roleDao.saveOrUpdate(xykAdmin);

		Trole dkAdmin = new Trole();
		dkAdmin.setId("dkAdmin");
		dkAdmin.setName("贷款管理员");
		dkAdmin.setTrole(superAdmin);// 父级是超管
		dkAdmin.setSeq(3);
		dkAdmin.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('dkgl') or t.id in ('dkgl')"));
		roleDao.saveOrUpdate(dkAdmin);

		Trole xtAdmin = new Trole();
		xtAdmin.setId("xtAdmin");
		xtAdmin.setName("系统管理员");
		xtAdmin.setTrole(superAdmin);// 父级是超管
		xtAdmin.setSeq(4);
		xtAdmin.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('xtgl') or t.id in ('yhgl')"));
		roleDao.saveOrUpdate(xtAdmin);

		Trole olyh = new Trole();
		olyh.setId("olyh");
		olyh.setName("一卡通用户");
		olyh.setTrole(superAdmin);// 父级是超管
		olyh.setSeq(5);
		olyh.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('olgl') or t.id in ('olgl')"));
		roleDao.saveOrUpdate(olyh);

		Trole olxykyh = new Trole();
		olxykyh.setId("olxykyh");
		olxykyh.setName("信用卡用户");
		olxykyh.setTrole(superAdmin);// 父级是超管
		olxykyh.setSeq(6);
		olxykyh.getTresources().addAll(resourceDao.find("from Tresource t where t.tresource.id in ('olxykgl') or t.id in ('olxykgl')"));
		roleDao.saveOrUpdate(olxykyh);
	}

	private void initUser() {
		List<Tuser> l = userDao.find("from Tuser t where t.name in ('jy','10000','20000','30000','40000','1000000000','2000000000')");
		if (l != null && l.size() > 0) {
			for (Tuser user : l) {
				userDao.delete(user);
			}
		}

		Tuser admin = new Tuser();
		admin.setId("0");
		admin.setType(0);
		admin.setName("jy");
		admin.setPwd("123456");
		admin.setCreatedatetime(new Date());
		admin.getTroles().addAll(roleDao.find("from Trole t"));// 给用户赋予所有角色
		userDao.saveOrUpdate(admin);

		Tuser admin1 = new Tuser();
		admin1.setId("1");
		admin1.setType(1);
		admin1.setName("10000");
		admin1.setPwd("123456");
		admin1.setCreatedatetime(new Date());
		admin1.getTroles().addAll(roleDao.find("from Trole t where t.id = 'cxAdmin'"));// 给用户赋予储蓄角色
		userDao.saveOrUpdate(admin1);

		Tuser admin2 = new Tuser();
		admin2.setId("2");
		admin2.setType(2);
		admin2.setName("20000");
		admin2.setPwd("123456");
		admin2.setCreatedatetime(new Date());
		admin2.getTroles().addAll(roleDao.find("from Trole t where t.id = 'xykAdmin'"));// 给用户赋予信用卡角色
		userDao.saveOrUpdate(admin2);

		Tuser admin3 = new Tuser();
		admin3.setId("3");
		admin3.setType(3);
		admin3.setName("30000");
		admin3.setPwd("123456");
		admin3.setCreatedatetime(new Date());
		admin3.getTroles().addAll(roleDao.find("from Trole t where t.id = 'dkAdmin'"));// 给用户赋予贷款角色
		userDao.saveOrUpdate(admin3);

		Tuser admin4 = new Tuser();
		admin4.setId("4");
		admin4.setType(4);
		admin4.setName("40000");
		admin4.setPwd("123456");
		admin4.setCreatedatetime(new Date());
		admin4.getTroles().addAll(roleDao.find("from Trole t where t.id = 'xtAdmin'"));// 给用户赋予系统角色
		userDao.saveOrUpdate(admin4);

		Tuser admin5 = new Tuser();
		admin5.setId("5");
		admin5.setType(5);
		admin5.setName("1000000000");
		admin5.setPwd("123456");
		admin5.setCreatedatetime(new Date());
		admin5.getTroles().addAll(roleDao.find("from Trole t where t.id = 'olyh'"));// 给用户赋予一卡通角色
		userDao.saveOrUpdate(admin5);

		Tuser guest = new Tuser();
		guest.setId("6");
		guest.setType(6);
		guest.setName("2000000000");
		guest.setPwd("123456");
		guest.setCreatedatetime(new Date());
		guest.getTroles().addAll(roleDao.find("from Trole t where t.id = 'olxykyh'"));// 给用户赋予信用卡角色
		userDao.saveOrUpdate(guest);
	}
}
